package com.power.powercash.report;

import com.power.powercash.ImageSet;
import com.power.visual.BlackButton;
import com.power.visual.PaintStyle;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author f6036477
 */
public class Column extends JPanel implements ListSelectionListener, MouseListener {

	private ColumnInfo rows;

	private JList body;

	private BlackButton head;

	private int height;

	private boolean sorted = false;


	@Override
	public void valueChanged(ListSelectionEvent e) {
		int[] is = this.getSelectedIndices();
		for(int i = 0; i < is.length; i++) {
			System.out.println("i: "+is[i]+"/o: "+this.getElementAt(is[i]));
			System.out.println(rows.getValues()[is[i]]);
		}//for
	}


	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getButton() == 3) {
			int i = body.locationToIndex(e.getPoint());
			if(!body.getSelectionModel().isSelectedIndex(i)) {
				this.select(i);
			}
			System.out.print("Right Click on: ");
			System.out.println(body.getModel().getElementAt(i));
		}
	}


	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}


	private static class ArrayListModel extends AbstractListModel {
		private Object[] objs;

		public ArrayListModel() {
			objs = null;
		}

		public ArrayListModel(Object[] os) {
			objs = os;
		}

		public void setObjects(Object[] os) { objs = os; }

		@Override
		public Object getElementAt(int index) {
			return (objs == null ? null :
				(index > objs.length || index < 0
				? null : objs[index]));
		}

		@Override
		public int getSize() {
			return (objs == null ? 0 : objs.length);
		}
	}


	private class Renderer extends JLabel implements ListCellRenderer {

		private Color back = new Color(0xe1e1e1);

		@Override
		public Component getListCellRendererComponent(
				JList list,
				Object value,
				int index,
				boolean isSelected,
				boolean cellHasFocus)
		{
			Dimension d = new Dimension(this.getWidth(), 22);
			this.setSize(d);
			this.setPreferredSize(d);
			this.setText(value.toString());
			this.setFont(list.getFont());
			this.setForeground(list.getForeground());
			this.setBackground(list.getBackground());
			this.setOpaque(true);

			if(isSelected) {
				this.setBackground(list.getSelectionBackground());
				this.setForeground(list.getSelectionForeground());
			} else if(index % 2 != 0) {
				this.setBackground(back);
			}
			return this;
		}

	}


	public Column() {
		rows = null;
		height = 0;
		body = new JList();
		body.setCellRenderer(new Renderer());
		head = new BlackButton(PaintStyle.GRADIENT_WINXP, "");
		head.setImageIcon(ImageSet.V);
		head.setHorizontalAlignment(SwingConstants.LEFT);
		head.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				setSorted(!sorted);
			}
		});
		body.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		body.addListSelectionListener(this);
		body.addMouseListener(this);
		setLayout(new BorderLayout(0, 0));
		add(head, BorderLayout.NORTH);
		//add(body, BorderLayout.CENTER);
	}


	public void setSorted(boolean s) {
				if(s) {
					head.setImageIcon(ImageSet.OV);
					body.setModel(toListModel(rows.sort()));
				} else {
					head.setImageIcon(ImageSet.V);
					body.setModel(toListModel(rows.reorder(rows.getOrder())));
				}
				sorted = s;
				body.repaint();
				head.repaint();
	}


	public static ListModel toListModel(Object[] os) {
		return new ArrayListModel(os);
	}


	public Column(String title) {
		this();
		head.setText(title);
	}


	public void setFixHeight(int height) {
		this.height = height;

		if(this.height > 0) {
			remove(body);
			Dimension d = new Dimension(getWidth(), height);
			setSize(d);
			setPreferredSize(d);
			JScrollPane scroll = new JScrollPane(body);
			d = new Dimension(d.width, d.height - head.getHeight() -1);
			scroll.setSize(d);
			scroll.setPreferredSize(d);
			add(scroll, BorderLayout.CENTER);
		}
	}


	public void setTitle(String title) {
		this.head.setText(title);
	}


	public String getTitle() {
		return head.getText();
	}


	public void setColumnInfo(ColumnInfo ci) {
		rows = ci;
		body.setModel(toListModel(rows.getValues()));
		add(body, BorderLayout.CENTER);
	}


	public ColumnInfo getColumnInfo() {
		return rows;
	}


	public void select(int ... index) {
		body.setSelectedIndices(index);
	}


	public Object getElementAt(int index) {
		if(index < 0 || index > body.getModel().getSize())
			return null;
		return body.getModel().getElementAt(index);
	}


	public int[] getSelectedIndices() {
		return body.getSelectedIndices();
	}


	public Object[] getSelectedObjects() {
		return body.getSelectedValues();
	}


	public void remove(int ... index) {
		for(int i = 0; i < index.length; i++) {
			Object o = rows.getValues()[index[i]];
			rows.removeExtracted(o);
		}//for
		body.setModel(toListModel(rows.renewValues()));
		body.repaint();
	}


	public static void main(String[] args) {
		class C {
			int i = 0;
			public int getI() { return i; }
		}

		class B {
			C c = new C();
			public C getC() { return c; }
		}

		class A {
			B b = new B();
			public B getB() { return b; }
		}

		A a1 = new A();
		A a2 = new A();
		A a3 = new A();
		a1.b.c.i = 100;
		a2.b.c.i = 20;
		a3.b.c.i = 3;

		Field f = new Field();
		f.addInfo(A.class, "getB", B.class);
		f.addInfo(B.class, "getC", C.class);
		f.addInfo(C.class, "getI", Integer.class);

		ColumnInfo coli = new ColumnInfo();
		coli.setField(f);
		coli.objects().add(a3);
		coli.objects().add(a1);
		coli.objects().add(a2);

		coli.getValues();

		for(int i = 0; i < coli.objects().size(); i++) {
			System.out.println(coli.getOrder()[i] + ". " + coli.getValues()[i]);
		}//for

		System.out.println("----");
		coli.sort();

		for(int i = 0; i < coli.objects().size(); i++) {
			System.out.println(coli.getOrder()[i] + ". " + coli.getValues()[i]);
		}//for

		System.out.println("----");

		coli.reorder(coli.getOrder());

		for(int i = 0; i < coli.objects().size(); i++) {
			System.out.println(coli.getOrder()[i] + ". " + coli.getValues()[i]);
		}//for

		JFrame frame = new JFrame();
		Column cola = new Column("A");
		Column colb = new Column("B");
		Column colc = new Column("C");
		cola.setColumnInfo(coli);
		colb.setColumnInfo(coli);
		colc.setColumnInfo(coli);

		JSplitPane splitc = new JSplitPane();
		splitc.setLeftComponent(colc);
		splitc.setRightComponent(new JPanel());
		splitc.setDividerSize(3);

		JSplitPane splitb = new JSplitPane();
		splitb.setLeftComponent(colb);
		splitb.setRightComponent(splitc);
		splitb.setDividerSize(3);

		JSplitPane splita = new JSplitPane();
		splita.setLeftComponent(cola);
		splita.setRightComponent(splitb);
		splita.setDividerSize(3);

		frame.add(splita);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

}
